<?php
class LbsModel
{
    private $diaryListCacheModel;
    private $lbsKeyPrefix = 'GEO/';
    private $cityHashPrefix='CHP/';
    function __construct(){
        $this->diaryListCacheModel=new DiaryListCacheModel();
    }
     
    /**
     * Diarygeo
     * @param unknown $cityid
     * @param unknown $id
     * @param unknown $length
     * @return multitype:unknown
     */
     public function getDiarysBodyIdByCity($cityid,$citycode,$id,$length){

        $lists  =   array();
        $result =   $this->diaryListCacheModel->getCityCache($cityid,$id,$length);
        
        foreach($result as $bodyId){
            $list['typeid'] = $bodyId;
            $lists[]        = $list;
        }   
         
        if(empty($result) && $id ==  0){
            $keys        =   $this->lbsKeyPrefix. rand(0,31);
            $where       =   array('citycode'=>$citycode,'status'=>'0','type'=>AC::LBS_TYPE_DIARY);
            $orderBy     =   " id DESC ";
            $fields      =   array('typeid','time');
            $res         =   Storage::getFieldsConditionManyData($keys, $fields, $where, $orderBy, 0, AC::LIMITLENGTHOFLIST);         
            $lists       =   array_slice($res,0,$length);            
            //写缓存
            $this->addCityCache($res, $cityid);
        }
        
        if(empty($lists)){
            return  array();
        }
        return $lists;
    }
    
    private function addCityCache($res, $cityid){
        if(!empty($res) || !empty($cityid)){
            foreach ($res as $k=>$v){
                $this->diaryListCacheModel->addCityCache($cityid, $v['typeid'], $v['time']);
            }
        }      
    }
    
    /**
     *增加geo
     */
    public function addDiaryGeo($info,$type)
    {
        $data   =array(
                'type'         => $type,
                'uid'          => $info['uid'],
                'typeid'       => $info['typeid'],//bodyid gid uid
                'province'     => isset($info['province']) ? $info['province'] : '',
                'city'         => $info['city'],
                'region'       => $info['region'],
                'name'         => isset($info['name']) ? $info['name'] : '',
                'address'      => isset($info['address']) ? $info['address'] : '',
                'latitude'     => $info['latitude'],
                'longitude'    => $info['longitude'],                         
                'status'       => 0,
                'time'         => AC::SERVER_TIME,           
                'citycode'     => md5($info['province'].$info['city'])    
        );

        //市geo信息
        $citys=array('北京市','上海市','天津市','重庆市');  
        if(in_array(trim($info['city']),$citys)){
            $data['citycode']=md5($info['city'].$info['region']); 
            $this->addCityCodeDiary($data, $info['city'], $info['uid']);
        } 
        $key    = $this->lbsKeyPrefix. $info['uid'];
        $result = Storage::addData($key, $data);         
        //写缓存
        $this->addBodyIdToCityFeedList($data['citycode'], $data['typeid']);
        
        //省geo信息
        $data['citycode'] = md5($info['province']);
        $this->addBodyIdToCityFeedList($data['citycode'], $data['typeid']);

        if (empty($result)){
            return false;
        }
        return true;
    }

    /**
     * 直辖市存市级信息
     * 
     */
    private function addCityCodeDiary($data, $city, $uid){
        if(empty($data) || empty($city) || empty($uid)) return false;
        $citys=array('北京市','上海市','天津市','重庆市');  
        if(in_array(trim($city),$citys)){
            $data['citycode']=md5($city.$city); 
        }
        $key    = $this->lbsKeyPrefix. $uid;
        $result = Storage::addData($key, $data);         
        //写缓存
        $this->addBodyIdToCityFeedList($data['citycode'], $data['typeid']);
        return true;
    }
      
    private function addBodyIdToCityFeedList($citycode, $bodyId){
        if(empty($citycode) || empty($bodyId)) return false;
            
        $cityid=$this->getGeoHash($citycode);
        if(!empty($cityid)){
            $this->diaryListCacheModel->addCityCache($cityid,$bodyId);
        }
        return true;
    }
    
    /**
     * 删除日记地理位置
     * 
     */
    public function removeDiaryGeo($info){
        
        isset($info['bodyId'])    &&    $where    =  array('typeid' => intval($info['bodyId']),'uid' => intval($info['uid']));
   
        $key    =   $this->lbsKeyPrefix. intval($info['uid']);
        $value  =   array('status'=>1);
        $result =   Storage::setConditionData($key, $value, $where);
        
        $res    =   Storage::getConditionManyData($key, $where);
        $cityid = array();
        foreach ($res as $val){
            $cityid[] =   $this->getGeoHash($val['citycode']);
        }
        
        if ($cityid) {
            foreach ($cityid as $v){
                if(!empty($v)){
                    $this->diaryListCacheModel->removeCityCache($v, $info['bodyId']);
                }
            }
        }
        
        if(empty($result)){
            return false;
        }
        
        return  true;
    }
   
    /**
     * 城市ID
     * @param unknown $citycode
     * @return unknown
     */
    public function getGeoHash($citycode){    
        $arrid =   $this->diaryListCacheModel->getCityIdCache(array($citycode));
        $id=$arrid[$citycode];
        if(empty($id)){
            $hash   =   $citycode;
            $where  =   array('hash'=>$hash);    
            $key    =   $this->cityHashPrefix. 0;
            $result =   Storage::getConditionData($key, $where);              
            if($result['id']){
                $this->diaryListCacheModel->addCityIdCache(array($hash=>$result['id']));
                $id =   $result['id'];
            }
        }
      
       if(empty($id)){
           return 0;
       } 
       return $id;
    }
    
    /**
     * 
     * @param unknown $province
     * @param unknown $city
     * @param unknown $region
     * @return number|unknown
     */
    public function getCityIdByCityCode($province,$city,$region){
    
        $citycode=md5($province.$city);
    
        $citys=array('北京市','上海市','天津市','重庆市');
        if(in_array(trim($diaryInfo['geo']['city']),$citys)){
            $citycode=md5($city.$region);
        }
    
        $cityid=$this->getGeoHash($citycode);
        if(empty($cityid)){
            return 0;
        }
        return $cityid;
    }
}